package com.meichis.yndms.ui.activity;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.widget.DatePicker;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.meichis.mcsappframework.utils.DateUtil;
import com.meichis.yndms.R;
import com.meichis.yndms.adapter.EventNotificationAdapter;
import com.meichis.yndms.databinding.ActivityEventNotificationBinding;
import com.meichis.yndms.ui.common.BaseActivity;
import com.meichis.yndms.vm.EventNotificationVm;

import java.util.ArrayList;
import java.util.Calendar;

public class EventNotificationActivity extends BaseActivity {

    ActivityEventNotificationBinding binding;
    private EventNotificationVm vm;
    private EventNotificationAdapter adapter;

    @Override
    protected void setDataBindingContentView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_event_notification);
        vm = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(EventNotificationVm.class);
        binding.setVm(vm);
        binding.setClick(new ClickEvent());
        binding.setLifecycleOwner(this);
    }

    @Override
    protected void init() {
        binding.navigation.txtTitle.setText("活动批复列表");
        binding.navigation.navBack.setOnClickListener(v -> onBackPressed());

        adapter = new EventNotificationAdapter(this, R.layout.item_event_notification, new ArrayList<>());
        binding.rc.setLayoutManager(new LinearLayoutManager(this));
        binding.rc.setAdapter(adapter);
        vm.getTpmAppSummary();

        addObserver();
    }

    private void addObserver() {
        vm.getShowDialog().observe(this, aBoolean -> showLoading(aBoolean));
        vm.getEventData().observe(this, eventNotifications -> adapter.setList(eventNotifications));
    }

    public class ClickEvent {

        public void selectBeginTime() {
            final Calendar cd = Calendar.getInstance();
            new DatePickerDialog(getActivity(), AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    cd.set(Calendar.YEAR, year);
                    cd.set(Calendar.MONTH, monthOfYear);
                    cd.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                    vm.getBeginTime().setValue(DateUtil.date2Str(cd, DateUtil.FORMAT_YMD));
                }
            }, cd.get(Calendar.YEAR), cd.get(Calendar.MONTH), cd.get(Calendar.DAY_OF_MONTH)).show();
        }

        public void selectEndTime() {
            final Calendar cd = Calendar.getInstance();
            new DatePickerDialog(getActivity(), AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    cd.set(Calendar.YEAR, year);
                    cd.set(Calendar.MONTH, monthOfYear);
                    cd.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                    vm.getEndTime().setValue(DateUtil.date2Str(cd, DateUtil.FORMAT_YMD));
                }
            }, cd.get(Calendar.YEAR), cd.get(Calendar.MONTH), cd.get(Calendar.DAY_OF_MONTH)).show();
        }

        public void searchEvents() {

        }

    }
}
